<template>
  <div id="customUnitReport">
    <el-dialog
      :title="dialogTitle"
      :visible.sync="formShowVisible"
      width="98%" top="20px" :close-on-click-modal="false" :close-on-press-escape="false">
      <unitFormShow :projectId="projectId" :timestamp="timestamp" :readOnly="readonly" @saveUnitData="saveUnitData" :unitData="unitData" :project="project" />
    </el-dialog>
    <el-dialog
      title="填报说明"
      :visible.sync="infoShowVisible"
      width="98%" top="20px" :close-on-click-modal="false" :close-on-press-escape="false">
      <div style="font-size:var(--size16);line-height:150%;margin:0 20px;text-indent:2em;">
        <p>1．统计数据截止时间为2025年6月30日。</p>
        <p>2．本表格主要统计以下纪检机构及其中专门从事或者主要从事纪检工作的在编人员情况：中央和国家机关各部门机关纪委，以及中央和国家机关各部门所属单位、管理（监管、代管）单位，垂直管理单位（实行双重领导并以上级单位领导为主的单位）各级内设纪检机构。</p>
        <p>3．中央和国家机关党委（党组）巡视巡察机构中，专责履行巡视巡察职责的编制及实有人员情况，纳入本次统计。如存在列编在纪检机构内或由纪检机构统一管理的情形（不包括仅由纪检机构主要负责人分管或协管的情形），<b>需附件细化说明</b>。中央一级党政机关、企事业单位的二级及以下单位内设巡察机构编制人员不纳入本次统计。</p>
        <p>4．以下机构、人员不列入统计范围：①未在纪律检查机关担负具体工作的纪委委员；②党总支、党支部的纪检委员； ③其他兼职纪检干部；④借调人员、挂职人员、临时抽调人员、编外聘用人员；⑤离退休纪检干部。未在各级纪检机关担负具体工作的纪委委员、纪检委员不列入此次统计范围。</p>
        <p>5．“编制总数”应等于“行政编制”“事业编制”“企业编制”之和；暂未核定编制的，编制数按实有人数填写。存在其他编制或暂未核定编制的，<b>需附件细化说明</b>。</p>
        <p>6．“实有人员”，按照“专门从事或者主要从事纪检工作”把握，如有特殊情况请另附材料细化说明。“实有人数” 应等于“行政人员”“事业人员”“企业人员”之和。在纪检监察系统内兼任多个职务的，按其最高职务层次统计一次。避免遗漏或重复统计。</p>
        <p>7．“领导职数”“实际配备情况”栏中，“按职务层次分”填写相关单位部门核定的领导职数及配备情况，存在一名领导干部兼任多个职务的，需分别体现配备情况。</p>
        <p>8．“年龄结构”（不含平均年龄）“最高学历结构”分别合计数均应与“实有人数”相同。</p>
        <p>9．“最高学历结构”按获得的最高学历填写。最高学历不包括仅获得学位、无相应学历的情形。“高中及以下”包括中专、中技等。</p>
        <p>10．“在岗人员严重疾病情况”栏中，主要统计目前患有重大疾病、严重精神疾病以及导致无法正常到岗工作的其他严重疾病情况。其中，“重大疾病”可参照中国保险行业协会、中国医师协会发布的重大疾病定义的有关规定。填写本栏<b>需附件细化说明</b>人员姓名、性别、年龄、职务职级、所患疾病等情况。</p>
        <p>11．“在岗人员伤残情况”统计持有残疾人证人员，填写本栏<b>需附件细化说明</b>人员姓名、性别、年龄、职务职级、伤残缘由、程度等情况。</p>
        <p>12．“在岗人员死亡情况”中，除烈士、因公牺牲以外情况的（如自杀身亡、病故），均在“其他”中填写。因工死亡人员在“因公牺牲”中填写。因公牺牲、因工死亡人员需经有关部门根据规定予以确认。填写以上3项<b>需附件细化说明</b>人员姓名、年龄、职务职级、死亡时间、原因等情况。</p>
        <p>13．“垂管单位”中，一并统计“双重领导并以上级单位领导为主”的情况。其中，垂管单位省级纪检机构“机构数”栏，统计实行垂直管理或者双重领导并以上级单位领导为主的中央和国家机关下一级单位纪检机构数量，不统计其他分支机构数量。</p>
        <p>14．如有其他情况，可附说明，请勿改变表格样式。</p>
      </div>
    </el-dialog>
    <el-breadcrumb separator=">">
      <!-- <el-breadcrumb-item :to="{ path: '/' }">系统首页</el-breadcrumb-item> -->
      <!-- <el-breadcrumb-item>人才建设</el-breadcrumb-item> -->
      <el-breadcrumb-item>统计填报</el-breadcrumb-item>
    </el-breadcrumb>
    <section class="wboxs">
    <!--工具条-->
      <h1>项目名称：{{project.title}}</h1>
      <el-popconfirm
        @confirm="saveData"
        @onConfirm="saveData"
        icon="el-icon-info"
        icon-color="red"
        title="确定提交所填信息吗？"
      >
        <el-button slot="reference" type="warning" :loading="dataLoading" icon="el-icon-s-promotion" style="float:right;margin:10px;">提交</el-button>
      </el-popconfirm>
      <el-button type="success" @click="addnew" icon="el-icon-plus" style="float:right;margin:10px 5px;">新增纪检机构</el-button>
      <el-button type="info" @click="showInfo" icon="el-icon-warning-outline" style="float:right;margin:10px 5px;">查看填报说明</el-button>
      <!-- <el-button type="primary" @click="initData" icon="el-icon-edit" style="float:right;margin:10px 5px;" :loading="initLoading" v-if="!listLoading&&list.length===0">用上一年度填报数据初始化</el-button> -->
      <div style="line-height:200%;margin:10px;">
        填报起止日期：{{project.beginDate}} 至 {{project.endDate}}
        <!-- <br>{{project.remark}} -->
      </div>
    </section>
    <section class="unitbox" style="margin-top:10px">
    <!-- <section style="margin-top:10px"> -->
      <!--列表-->
      <el-table :data="list" highlight-current-row v-loading="listLoading" style="width: 100%;" :height="tableHeight" border
        ref="multipleTable"
        >
        <el-table-column type="index" label="序号" width="60" align="center" class-name="tableth">
        </el-table-column>
        <el-table-column label="承担纪检工作机构名称" min-width="200" prop="deptName" />
        <el-table-column label="机构类型" width="200" prop="deptType" />
        <el-table-column label="行政编制" width="110" prop="unitOrg1" align="center" />
        <el-table-column label="事业编制" width="110" prop="unitOrg2" align="center" />
        <el-table-column label="企业编制" width="110" prop="unitOrg3" align="center" />
        <el-table-column label="编制总数" prop="allOrg" width="110" align="center">
          <template slot-scope="scope">
            {{(scope.row.unitOrg1?scope.row.unitOrg1:0)+(scope.row.unitOrg2?scope.row.unitOrg2:0)+(scope.row.unitOrg3?scope.row.unitOrg3:0)}}
          </template>
        </el-table-column>
        <el-table-column label="在岗人数" width="110" prop="livePersonNum" align="center" />
        <el-table-column label="死亡人数" width="110" prop="deathPersonNum" align="center" />
        <!-- <el-table-column label="人员交流人数" prop="allOrg" width="110" align="center" v-if="project.projectVersion&&project.projectVersion==='v2'">
          <template slot-scope="scope">
            {{(scope.row.num1_1?scope.row.num1_1:0)+(scope.row.num1_2?scope.row.num1_2:0)}}
          </template>
        </el-table-column>
        <el-table-column label="在岗人员严重疾病人数" prop="allOrg" width="110" align="center" v-if="project.projectVersion&&project.projectVersion==='v2'">
          <template slot-scope="scope">
            {{scope.row.num2_1?scope.row.num2_1:0}}
          </template>
        </el-table-column>
        <el-table-column label="在岗人员伤残人数" prop="allOrg" width="110" align="center" v-if="project.projectVersion&&project.projectVersion==='v2'">
          <template slot-scope="scope">
            {{scope.row.num3?scope.row.num3:0}}
          </template>
        </el-table-column>
        <el-table-column label="在岗人员死亡人数" prop="allOrg" width="110" align="center" v-if="project.projectVersion&&project.projectVersion==='v2'">
          <template slot-scope="scope">
            {{(scope.row.num4_1?scope.row.num4_1:0)+(scope.row.num4_2?scope.row.num4_2:0)+(scope.row.num4_3?scope.row.num4_3:0)+(scope.row.num4_4?scope.row.num4_4:0)}}
          </template>
        </el-table-column>
        <el-table-column label="机构奖励数量" width="110" prop="awards" align="center">
          <template slot-scope="scope">
            {{scope.row.awards?scope.row.awards.length:0}}
          </template>
        </el-table-column>
        <el-table-column label="实有人数" width="110" prop="personNum" align="center" /> -->
        <el-table-column label="操作" width="410" align="center">
          <template slot-scope="scope">
            <el-button @click="detail(scope.row.id)" type="success" size="mini" style="margin:10px 0 10px 5px">在岗人员信息</el-button>
            <el-button @click="detailDeath(scope.row.id)" type="warning" size="mini" style="margin:10px 0 10px 5px">死亡人员信息</el-button>
            <el-button @click="edit(scope.row)" type="primary" size="mini" style="margin:10px 0 10px 5px">修改</el-button>
            <el-popconfirm
              @confirm="del(scope.row.id)"
              @onConfirm="del(scope.row.id)"
              icon="el-icon-info"
              icon-color="red"
              title="确定删除本条信息吗？"
            >
              <el-button type="danger" slot="reference" size="mini">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!-- <div class="block">
        <el-pagination :total="totalnum" :page-size="pageSize" @current-change="handleCurrentChange" :current-page="pageNum" @size-change="handleSizeChange" :page-sizes="[10, 20, 30, 50]" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
      </div> -->
    </section>
  </div>
</template>

<script>
import formShow from './customFormShow2025'
import unitFormShow from './customUnitFormShow2025'
export default {
  components: {
    formShow, unitFormShow
  },
  data () {
    return {
      initLoading: false,
      unitDataFlag: false,
      unitData: {},
      infoShowVisible: false,
      unitInfoVisible: false,
      pageNum: 1,
      pageSize: 20,
      totalnum: 0,
      Isace: true,
      Sortkey: '',
      limitNum: '',
      index: -1,
      formShowVisible: false,
      timestamp: '',
      projectId: null,
      dialogTitle: '新增',
      items: [],
      unitname: '',
      project: {},
      listLoading: true,
      dataLoading: true,
      tableHeight: 100,
      list: [
      ],
      changeSizeFlag: false,
      readonly: false,
      item: {},
      fileIds: []
    }
  },
  methods: {
    initData () {
      this.initLoading = true
      this.$http
        .get(this.$http.defaults.unitReportAPI + `api/project/saveInitProjectData?projectDataId=` + this.$route.params.id)
        .then(response => {
          this.initLoading = false
          if (response.data.success) {
            this.$message({
              message: '初始化成功',
              type: 'success'
            })
            this.getProjectDataDetail(this.$route.params.id)
          } else {
            this.$message({
              message: '初始化失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          this.initLoading = false
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
        })
    },
    detail (id) {
      this.$router.push('/unit/customDeptDetail2025/' + this.$route.params.id + ',' + id)
    },
    detailDeath (id) {
      this.$router.push('/unit/customDeptDetailDeath2025/' + this.$route.params.id + ',' + id)
    },
    handleSizeChange (val) {
      this.changeSizeFlag = true
      this.pageNum = 1
      this.pageSize = val
      this.getPrj()
    },
    handleCurrentChange (val) {
      if (this.changeSizeFlag === false) {
        this.pageNum = val
        this.getPrj()
      }
    },
    del (id) {
      let para = {
        projectDataId: parseInt(this.$route.params.id),
        pageNum: 1,
        pageSize: 1,
        deptId: id
      }
      // var personParams = { Page: 0, Pagenum: 20, Personname: '', Unitkey: '', Sortkey: '', Isace: true, timestamp: new Date().getTime() }
      this.$http
        .post(this.$http.defaults.unitReportAPI + `api/project/queryDataItemByPage2005`, para)
        .then(response => {
          if (response.data.result.total) {
            this.$message({
              message: '该机构下已填报人员信息，无法删除。请先点击查看该机构人员信息，删除该机构下全部人员，再进行删除机构操作',
              type: 'error'
            })
          } else {
            this.list.forEach((item, index, arr) => {
              if (item.id && item.id === id) {
                arr.splice(index, 1)
              }
            })
            this.updataUnitData()
          }
        })
        .catch(error => {
          this.$message({
            message: '获取失败:' + error,
            type: 'error'
          })
        })
    },
    showInfo () {
      this.infoShowVisible = true
    },
    setUnitInfo () {
      this.unitInfoVisible = true
    },
    addData (item) {
      this.formShowVisible = false
      this.fileIds = []
      let para = {
        id: item.id,
        dataJson: JSON.stringify(item),
        fileIds: this.fileIds,
        projectDataId: parseInt(this.$route.params.id),
        ageDate: '20250630'
      }
      this.$http
        .post(this.$http.defaults.unitReportAPI + `api/project/saveDataItem2025`, para)
        .then(response => {
          if (response.data.success) {
            this.fileIds = []
            this.getProjectDataDetail(this.$route.params.id)
            this.$message({
              message: '保存成功',
              type: 'success'
            })
          } else {
            this.$message({
              message: '保存失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          this.$message({
            message: '网络保存失败:' + error,
            type: 'error'
          })
        })
      // if (this.index >= 0) {
      //   this.list.splice(this.index, 1, item)
      // } else {
      //   this.list.push(item)
      // }
      // this.saveData(0)
    },
    saveData () {
      if (!this.list || this.list.length === 0) {
        this.$message({
          message: '未添加纪检机构信息',
          type: 'error'
        })
        return false
      }
      let errorNum = 0
      let errortext1 = ''
      this.list.forEach((item, index) => {
        let errorFlag = false
        if (!item.num1_1 && !item.num1_2 && item.content1) {
          errortext1 = '机构“' + item.deptName + '”人员交流情况描述不为空，但人员交流人数为0'
          errorFlag = true
        }
        if (!item.num2_1 && !item.num2_2 && item.content2) {
          errortext1 = '机构“' + item.deptName + '”在岗人员严重疾病情况描述不为空，但在岗人员严重疾病人数为0'
          errorFlag = true
        }
        if (!item.num3 && item.content3) {
          errortext1 = '机构“' + item.deptName + '”在岗人员伤残情况描述不为空，但在岗人员伤残人数为0'
          errorFlag = true
        }
        if (!item.num4_1 && !item.num4_2 && !item.num4_3 && !item.num4_4 && item.content4) {
          errortext1 = '机构“' + item.deptName + '”在岗人员死亡情况描述不为空，但在岗人员死亡人数为0'
          errorFlag = true
        }
        if (errorFlag) { errorNum++ }
      })
      if (errorNum > 0) {
        this.$message({
          message: errorNum + '家纪检机构填写有误，如：' + errortext1,
          type: 'error'
        })
      } else {
        let para = {
          projectDataId: parseInt(this.$route.params.id),
          state: 1
        }
        console.log(para)
        this.$http
          .post(this.$http.defaults.unitReportAPI + `api/project/saveProjectData`, para)
          .then(response => {
            if (response.data.success) {
              this.$message({
                message: '提交成功',
                type: 'success'
              })
              this.$router.push('/unit/unitReport')
            } else {
              this.$message({
                message: '提交失败:' + response.data.message,
                dangerouslyUseHTMLString: true,
                type: 'error'
              })
            }
          })
          .catch(error => {
            this.$message({
              message: '网络提交失败:' + error,
              type: 'error'
            })
          })
      }
    },
    saveUnitData (data) {
      if (data.id) {
        this.list.forEach((item, index, arr) => {
          if (item.id && item.id === data.id) {
            arr[index] = data
          }
        })
      } else {
        this.list.push(data)
      }
      this.updataUnitData()
    },
    updataUnitData () {
      this.listLoading = true
      let para = {
        id: parseInt(this.$route.params.id),
        dataJson: JSON.stringify(this.list)
      }
      this.$http
        .post(this.$http.defaults.unitReportAPI + `api/busProjectData/save`, para)
        .then(response => {
          this.listLoading = false
          if (response.data.success) {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.list = response.data.result
            this.formShowVisible = false
            // this.getProjectDataDetail(this.$route.params.id)
          } else {
            this.$message({
              message: '操作失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          this.listLoading = false
          this.$message({
            message: '网络操作失败:' + error,
            type: 'error'
          })
        })
    },
    addnew () {
      this.dialogTitle = '新增纪检机构信息'
      this.index = -1
      this.timestamp = new Date().getTime()
      this.formShowVisible = true
      this.unitData = {
        deptName: '',
        deptType: '',
        unitOrg1: 0,
        unitOrg2: 0,
        unitOrg3: 0,
        num1_1: 0,
        num1_2: 0,
        num2_1: 0,
        num2_2: 0,
        num3: 0,
        num4_1: 0,
        num4_2: 0,
        num4_3: 0,
        num4_4: 0,
        content1: '',
        content2: '',
        content3: '',
        content4: '',
        l1: 0,
        l2: 0,
        l3: 0,
        l4: 0,
        l5: 0,
        l6: 0,
        l7: 0,
        lz: 0,
        lf: 0,
        fileList1: [],
        fileList2: []
      }
    },
    edit (item) {
      this.dialogTitle = '编辑纪检机构信息'
      this.timestamp = new Date().getTime()
      this.formShowVisible = true
      this.unitData = item
    },
    getProjectDetail (id) {
      this.dataLoading = true
      this.listLoading = true
      this.$http
        .get(this.$http.defaults.unitReportAPI + `api/project/getProjectById?id=` + id)
        .then(response => {
          this.project = response.data.result
          if (this.project.projectVersion === 'v2') {
            this.list.forEach((item, index) => {
              if (!('num1_1' in item)) {
                this.list[index].num1_1 = 0
                this.list[index].num1_2 = 0
                this.list[index].num2_1 = 0
                this.list[index].num2_2 = 0
                this.list[index].num3 = 0
                this.list[index].num4_1 = 0
                this.list[index].num4_2 = 0
                this.list[index].num4_3 = 0
                this.list[index].num4_4 = 0
              }
            })
          }
          this.dataLoading = false
          this.listLoading = false
        })
        .catch(error => {
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
          this.dataLoading = false
          this.listLoading = false
        })
    },
    getLivePersonNum (id, index) {
      let para = {
        projectDataId: this.$route.params.id,
        pageNum: 1,
        pageSize: 1,
        deptId: id,
        liveFlag: 1
      }
      this.$http
        .post(this.$http.defaults.unitReportAPI + `api/project/queryDataItemByPage2005`, para)
        .then(response => {
          this.$set(this.list[index], 'livePersonNum', response.data.result.total)
        })
        .catch(error => {
          console.log(error)
          return 0
        })
    },
    getDeathPersonNum (id, index) {
      let para = {
        projectDataId: this.$route.params.id,
        pageNum: 1,
        pageSize: 1,
        deptId: id,
        liveFlag: 0
      }
      this.$http
        .post(this.$http.defaults.unitReportAPI + `api/project/queryDataItemByPage2005`, para)
        .then(response => {
          this.$set(this.list[index], 'deathPersonNum', response.data.result.total)
        })
        .catch(error => {
          console.log(error)
          return 0
        })
    },
    getProjectDataDetail (id) {
      this.$http
        .get(this.$http.defaults.unitReportAPI + `api/project/getProjectDataById?id=` + id)
        .then(response => {
          // if (response.data.result.dataJson) {
          //   this.list = JSON.parse(response.data.result.dataJson)
          // }
          this.projectId = response.data.result.projectId
          if (response.data.result.dataJson) {
            this.list = JSON.parse(response.data.result.dataJson)
            this.list.forEach((item, index) => {
              this.getLivePersonNum(item.id, index)
              this.getDeathPersonNum(item.id, index)
            })
          }
          this.getProjectDetail(this.projectId)
        })
        .catch(error => {
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
        })
    },
    tableresize (obj) {
      var clientHeight = 0
      if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      } else {
        clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      }
      let myheight = 249

      obj.$nextTick(function () {
        obj.tableHeight = clientHeight - myheight
      })
    }
  },
  mounted () {
    this.unitname = localStorage.getItem('Otherunitname')
    this.getProjectDataDetail(this.$route.params.id)
    this.tableresize(this)
    window.onresize = () => {
      this.tableresize(this)
    }
  }
}
</script>
<style>
#customUnitReport .el-drawer__wrapper{
  width: 100%;
  height: 100%;
  position: fixed !important;
  overflow: hidden;
}
  .unitbox .el-table th{
    font-weight: normal;
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
  }
  .unitbox .el-table td, .el-table th{
    padding:0;
    font-size:var(--size14) !important;
    line-height: 20px;
    font-family: '宋体'
  }
  .tableth{
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
    height:33px;
  }
  #testlist .wboxs{
    height:45px;
    padding-top:10px;
  }
  #customUnitReport .el-table td{
    border-right: 1px solid #95B8E7;
    border-bottom: 1px solid #95B8E7;
  }
  #customUnitReport .el-table__cell{
    border-right: 1px solid #95B8E7 !important;
    border-bottom: 1px solid #95B8E7 !important;
  }
</style>
<style scoped>
  #customUnitReport .el-button{
    color:#fff !important;
  }
  #customUnitReport h1{
    font-size:var(--size16) !important;
    font-weight: bolder;
    height: 30px;
    border-bottom: 1px solid #95B8E7;
    font-family: '宋体';
    line-height: 30px;
    color: #000;
    padding-left:10px;
    background-image: linear-gradient(to bottom , #EEF4FF, #E0ECFF);
    margin:0;
  }
  .el-pagination{
    float: left;
    padding:7px 10px;
  }
  #customUnitReport .wboxs{
    width: 100%;
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
    padding:0;
  }
  .unitbox{
    padding:0;
    width: calc(100% - 0px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
</style>
